using System;
using System.Collections.Generic;
using System.Text;

namespace GeoXML.SocialServices
{
    public static class Functions
    {
        /// <summary>
        /// Get average
        /// </summary>
        public static double GetAverage(double[] data)
        {
            int len = data.Length;
            if (len == 0)
                throw new Exception("No data");

            return GetSum(data) / len;
        }

        /// <summary>
        /// Get Sum
        /// </summary>
        public static double GetSum(double[] data)
        {
            if (data.Length == 0)
                throw new Exception("No Data");

            double sum = 0;
            for (int i = 0; i < data.Length; i++)
                sum += data[i];
            return sum;
        }

        /// <summary>
        /// Get variance
        /// </summary>
        public static double GetVariance(double[] data)
        {
            int len = data.Length;
            // Get average

            double avg = GetAverage(data);

            double sum = 0;
            for (int i = 0; i < data.Length; i++)
                sum += Math.Pow((data[i] - avg), 2);
            return sum / len;
        }

        /// <summary>
        /// Get standard deviation
        /// </summary>
        public static double GetStdev(double[] data)
        {
            return Math.Sqrt(GetVariance(data));
        } 

 
        /// <summary>
        /// Get correlation
        /// </summary>
        public static void GetCorrelation(double[] x, double[] y, ref double covXY, ref double pearson)
        {
            if (x.Length != y.Length)
                throw new Exception("Length of sources is different");
            double avgX = GetAverage(x);
            double stdevX = GetStdev(x);
            double avgY = GetAverage(y);
            double stdevY = GetStdev(y);
            int len = x.Length;

            for (int i = 0; i < len; i++)
                covXY += (x[i] - avgX) * (y[i] - avgY);
            covXY /= len;
            pearson = covXY / (stdevX * stdevY);
        }

        /// <summary>
        /// Deletes all the references in strText of strFilters, which are keywords separated
        /// by a ",".
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="strFilters"></param>
        /// <returns></returns>
        public static string StringFilter(string strText, string strFilters)
        {
            string strResult = null;
            string[] strComa = new string[1];
            strComa[0] = ",";

            string[] strFilterArray = strFilters.Split(strComa, StringSplitOptions.RemoveEmptyEntries);
            strResult = strText;
            foreach (string strFilter in strFilterArray)
            {
                strResult = strResult.Replace(strFilter.ToLower(), "");
            }
            return strResult;
        }
    }
}
